package com.toutoushidao.ssm.service;

import com.mongodb.BasicDBObject;
import com.toutoushidao.ssm.dao.MongoDbDao;
import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class MongodbService {

  @Autowired public MongoDbDao mongodbdao;

  /**
   * 获取当前表下所有数据
   *
   * @param table
   * @return
   */
  public List<Map<String, String>> queryAll(String table) {
    return mongodbdao.selectAll(table);
  }

  /**
   * 插入数据
   *
   * @param table
   * @param document
   * @return
   */
  public boolean insert(String table, Document document) {
    return mongodbdao.insert(table, document);
  }

  /**
   * 删除一个
   *
   * @param table
   * @param document
   * @return
   */
  public boolean deleteOne(String table, BasicDBObject document) {
    return mongodbdao.deleteOne(table, document);
  }

  /**
   * 删除所有
   *
   * @param table
   * @param document
   * @return
   */
  public boolean delete(String table, BasicDBObject document) {
    return mongodbdao.delete(table, document);
  }

  /**
   * 更新一个
   *
   * @param table
   * @param whereDoc
   * @param updateDoc
   * @return
   */
  public boolean updateOne(String table, BasicDBObject whereDoc, BasicDBObject updateDoc) {
    return mongodbdao.updateOne(table, whereDoc, updateDoc);
  }

  /**
   * 更新所有
   *
   * @param table
   * @param whereDoc
   * @param updateDoc
   * @return
   */
  public boolean update(String table, BasicDBObject whereDoc, BasicDBObject updateDoc) {
    return mongodbdao.update(table, whereDoc, updateDoc);
  }
}
